﻿using System;
using System.Collections.Generic;
using System.Text;

namespace OpenWings.Entities
{
    public class Entity : IDirty
    {
        public event EventHandler<DirtyChangedEventArgs> DirtyChanged;
        public event EventHandler<DirtyTrackingChangedEventArgs> DirtyTrackingChanged;

        bool _IsDirty;
        bool _DirtyTrackingEnabled;

        public bool DirtyTracking
        {
            get
            {
                return _DirtyTrackingEnabled;
            }
            set
            {
                if (value != _DirtyTrackingEnabled)
                {
                    _DirtyTrackingEnabled = value;
                    OnDirtyTrackingChanged(this, new DirtyTrackingChangedEventArgs(_DirtyTrackingEnabled));
                }
            }
        }

        public bool IsDirty
        {
            get
            {
                return _IsDirty;
            }
            set
            {
                if (DirtyTracking)
                    if (_IsDirty != value)
                    {
                        _IsDirty = value;
                        OnDirtyChanged(this, new DirtyChangedEventArgs(_IsDirty));
                    }
            }
        }

        public virtual void Clean()
        {
            IsDirty = false;
        }

        public virtual void OnDirtyChanged(object sender, DirtyChangedEventArgs e)
        {
            if (DirtyChanged != null)
                DirtyChanged(sender, e);
        }

        public virtual void OnDirtyTrackingChanged(object sender, DirtyTrackingChangedEventArgs e)
        {
            if (DirtyTrackingChanged != null)
                DirtyTrackingChanged(sender, e);
        }
    }
}